﻿//
//  Monkey Manager
//  Copyright (C) 2012 Victor Tereschenko
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading;

using MonkeyManager;

namespace AddManager
{
    /// <summary>
    /// Event about file has hashing finish.
    /// </summary>
    public class FileHashedEventArgs : EventArgs
    {
        public FileHashedEventArgs(string fileName, string tth)
        {
            FileName = fileName;
            TTH = tth;
        }

        public string FileName { get; private set; }
        public string TTH { get; private set; }
    }

    /// <summary>
    /// Event about file yet to be hashed
    /// </summary>
    public class FileHashingEventArgs : EventArgs
    {
        public FileHashingEventArgs(string fileName)
        {
            FileName = fileName;
        }

        public string FileName { get; private set; }
    }
    // TODO: move to utils
    /// <summary>
    /// Calculates hashes for files in seperate thread.
    /// </summary>
    class HashCalculator : IDisposable
    {
        public HashCalculator()
        {
            mEvent = new ManualResetEvent(false);
            mThread = new Thread(calcHashes);
            mThread.Start();
        }

        Thread mThread;
        ManualResetEvent mEvent;
        ConcurrentQueue<string> mFilesToHash = new ConcurrentQueue<string>();

        public EventHandler<FileHashedEventArgs> FileHashed;
        public EventHandler<FileHashingEventArgs> FileHashing;

        public void Add(string file)
        {
            mFilesToHash.Enqueue(file);
            mEvent.Set();
        }

        public bool IsEmptyQueue { get { return mFilesToHash.IsEmpty; } }

        void calcHashes()
        {
            while (true)
            {
                string fileName;
                while (mFilesToHash.TryDequeue(out fileName))
                {
                    FileHashing(this, new FileHashingEventArgs(fileName));
                    var tth = Hasher.GetTTH(fileName);
                    FileHashed(this, new FileHashedEventArgs(fileName, tth));
                }
                mEvent.WaitOne();
                mEvent.Reset();
            }
        }

        bool mDisposing = false;
        public void Dispose()
        {
            if (mDisposing)
                return;
            mDisposing = true;

            mThread.Abort();
        }
    }
}
